#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <iostream>
#include <dirent.h>
#include <string.h>

#include <io.h>
#include <fcntl.h>
#include <tchar.h>
#include <process.h>

struct _PROGRAM_STACK;
struct _PROGRAM_FUNCS_MAP;
struct _PROGRAM_FUNCS_CURRENT;
struct _PROGRAM_VAR;
struct _PROGRAM_LINES;
struct _PROGRAM_COMMANDS;

struct _PROGRAM_STACK{
	int lineCount;
	int mainFunc;

	void *retVal;

	_PROGRAM_VAR *parametersStack;
	_PROGRAM_FUNCS_CURRENT *pCurrentFunc;

	_PROGRAM_FUNCS_MAP *pMainMap;
	_PROGRAM_FUNCS_MAP *pMap;
	_PROGRAM_FUNCS_MAP *lastMap;

	_PROGRAM_VAR *pVars;
	_PROGRAM_VAR *lastVar;

	_PROGRAM_LINES *pLines;
	_PROGRAM_LINES *lastLine;

	_PROGRAM_COMMANDS *cCommands;
};

struct _PROGRAM_FUNCS_MAP{
	int lineNum;
	int endLineNum;

	_PROGRAM_LINES *openLine;
	
	char *funcName;
	int numArgs;
	char *argName[10];
	int argType[10];

	_PROGRAM_FUNCS_MAP *next;
	_PROGRAM_FUNCS_MAP *prev;
};

struct _PROGRAM_FUNCS_CURRENT{
	_PROGRAM_FUNCS_MAP *pFunc;
	_PROGRAM_FUNCS_CURRENT *prev;
};

struct _PROGRAM_VAR{
	int varType;

	int arrayType; //array type
	int arrayMaxLen;
	_PROGRAM_VAR *arrayValue;
	_PROGRAM_VAR *lastArrayValue;
	//
	//1. INT
	//2. FLOAT
	//3. DOUBLE
	//4. CHAR
	//5. ARRAY 
	//
	char *varName;
	void *varValue;
	char varRange[500];

	_PROGRAM_VAR *next;
	_PROGRAM_VAR *prev;
};

struct _PROGRAM_LINES{
	int lineNum;
	char *lineLine;
	int lineType;
	//
	//1. Simple command
	//2. Function start
	//3. Function end
	//4. Composed command
	//
	int lineComposedReference;

	_PROGRAM_LINES *next;
	_PROGRAM_LINES *prev;
};

int clearSpaces(char *inStr, int ignore);
int composedStringDelimit(char *inStr, char delimiter, int position);

char *getValue(char *inStr, int lineNum);
int lineParser(_PROGRAM_LINES *pLine);
void executeFunction(_PROGRAM_FUNCS_MAP *fMap);
void executeProgram( char *fName );
void CreateConsole();

